#include "SDLResources.h"

SDLResources* SDLResources::instance = nullptr;

//using namespace tinyxml2;

SDLResources::SDLResources()
{
	loadedTextures = std::map<std::string, SDL_Texture*>();
}

SDLResources::~SDLResources()
{
	// delete tous les Textures
	for (std::map<std::string, SDL_Texture*>::iterator it = loadedTextures.begin(); it!=loadedTextures.end(); ++it)
	{
		SDL_DestroyTexture(it->second);
	}

	// delete tous les musics
	for (std::map<std::string, Mix_Music*>::iterator it = loadedMusics.begin(); it!=loadedMusics.end(); ++it)
	{
		//Mix_FreeMusic(it->second);
	}

	// delete tous les sons
	for (std::map<std::string, Mix_Chunk*>::iterator it = loadedSounds.begin(); it!=loadedSounds.end(); ++it)
	{
		Mix_FreeChunk(it->second);
	}

	// delete tous les Fonts
	for (std::map<std::string, TTF_Font*>::iterator it = loadedFonts.begin(); it!=loadedFonts.end(); ++it)
	{
		TTF_CloseFont(it->second);
	}
}

SDL_Texture* SDLResources::GetTexture(std::string fileName)
{
	if(loadedTextures[fileName] == nullptr)
	{
		loadedTextures[fileName] = LoadImage(fileName, SDLRenderer::GetInstance()->GetRenderer());
	}
	return loadedTextures[fileName];

}

Mix_Music* SDLResources::GetMusic(std::string fileName)
{
	if(loadedMusics[fileName] == nullptr)
	{
		loadedMusics[fileName] = LoadMusic(fileName);
	}
	return loadedMusics[fileName];
}

Mix_Chunk* SDLResources::GetSound(std::string fileName)
{
	if(loadedSounds[fileName] == nullptr)
	{
		loadedSounds[fileName] = LoadSound(fileName);
	}
	return loadedSounds[fileName];
}


TTF_Font* SDLResources::GetFont(std::string fileName, int fontSize)
{
	std::stringstream ss;
	ss << fontSize;
	fileName.append(ss.str());

	if(loadedFonts[fileName] == nullptr)
	{
		loadedFonts[fileName] = LoadFont(fileName, fontSize);
	}

	return loadedFonts[fileName];
}

SDL_Texture* SDLResources::LoadImage(std::string file, SDL_Renderer* renderer)
{
	SDL_Texture* loadedImage = nullptr;
	loadedImage = IMG_LoadTexture(renderer, file.c_str());
	if(loadedImage == nullptr)
	{
		throw std::runtime_error("Failed to load image: " + file + IMG_GetError());
	}
	return loadedImage;
}

Mix_Music* SDLResources::LoadMusic(std::string file)
{
	Mix_Music* loadedMusic = nullptr;
	loadedMusic = Mix_LoadMUS(file.c_str());
	if(loadedMusic == nullptr)
	{
		throw std::runtime_error("Failed to load music: " + file + Mix_GetError());
	}

	return loadedMusic;
}

Mix_Chunk* SDLResources::LoadSound(std::string file)
{
	Mix_Chunk* loadedSound = nullptr;
	loadedSound = Mix_LoadWAV(file.c_str());
	if(loadedSound == nullptr)
	{
		throw std::runtime_error("Failed to load sound: " + file + Mix_GetError());
	}

	return loadedSound;
}


TTF_Font* SDLResources::LoadFont(std::string file, int fontSize)
{
	TTF_Font* loadedFont = nullptr;
	std::string vraiFile;
	//loadedFont = new TTF_Font();

	//std::string vraiFile;
	vraiFile = file;
	std::stringstream ss;
	ss << fontSize;
	
	int size = (ss.str()).length();

	for(int i = 0; i < size; i++)
	{
		vraiFile.pop_back();
	}

	loadedFont = TTF_OpenFont(vraiFile.c_str(), fontSize);

	if(loadedFont == nullptr)
	{
		throw std::runtime_error("Failed to load font: " + file + TTF_GetError());
	}

	return loadedFont;
}